/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.tads.ghtads.view;

import br.ufpr.tads.ghtads.controller.Materia;
import br.ufpr.tads.ghtads.controller.Professor;
import br.ufpr.tads.ghtads.model.EnderecoDto;
import br.ufpr.tads.ghtads.model.MateriaDto;
import br.ufpr.tads.ghtads.model.ProfessorDao;
import br.ufpr.tads.ghtads.model.ProfessorDto;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author julio
 */
@WebServlet(name = "professor", urlPatterns = {"/professor"})
public class professor extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String[] tokens;
            StringBuilder prefweek = new StringBuilder();
            ProfessorDto prof = new ProfessorDto();
            EnderecoDto end = new EnderecoDto();
            List<MateriaDto> listMat = new ArrayList();
            Professor professor =  new Professor();
            String metodo = request.getParameter("metodo");
            System.out.println(metodo);
            if(metodo.equalsIgnoreCase("add")){
                prof.setNome(request.getParameter("nome"));
                prof.setTelefone(Integer.parseInt(request.getParameter("telefone")));
                prof.setMateriaprincipal(Integer.parseInt(request.getParameter("materiapricipal")));
                prof.setRg(Integer.parseInt(request.getParameter("rg")));
                prof.setSenha(request.getParameter("senha"));
                prof.setEmail(request.getParameter("email"));
                    end.setLogradouro(request.getParameter("Logradouro"));
                    end.setNumero(Integer.parseInt(request.getParameter("numero")));
                    end.setCep(request.getParameter("cep"));
                    end.setBairro(request.getParameter("bairro"));
                    end.setCidade(request.getParameter("cidade"));
                    end.setUf(request.getParameter("UF"));
                prof.setEndereco(end);
                   tokens = request.getParameter("mat").split(";");
                   String lmat = request.getParameter("mat");
                   for (String i:tokens) {
                       MateriaDto mat =  new MateriaDto(); 
                       mat.setId(Integer.parseInt(i));
                       listMat.add(mat);
                    }
                   Materia ma = new Materia();
               
                
                  
                   for (int i = 0; i < 5-tokens.length; i++) {
                       lmat = lmat+";0";
                   }
                 prof.setMateria(listMat);
                 
                  prefweek.append(request.getParameter("segunda")).append(",").append(request.getParameter("terca")).append(",").append(request.getParameter("quarta")).append(",").append(request.getParameter("quinta")).append(",").append(request.getParameter("sexta")).append(",").append(request.getParameter("sabado")).append(",").append(request.getParameter("segundaa")).append(",").append(request.getParameter("tercaa")).append(",").append(request.getParameter("quartaa")).append(",").append(request.getParameter("quintaa")).append(",").append(request.getParameter("sextaa")).append(",").append(request.getParameter("sabadoa")).append(";");
                 prof.setSemanaRestricao(prefweek.toString());
                 
                prof.setListamateria(lmat);
                try {
                    professor.incluir(prof);
                } catch (FileNotFoundException | ClassNotFoundException | SQLException ex) {
                    out.println(ex);
                    System.out.println(ex);
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
